import axios, { type AxiosResponse } from 'axios'
import { showToast } from 'vant'
const instance = axios.create({
  baseURL: import.meta.env.VITE_HTTP_BASE_URL,
})

// Add a request interceptor
axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    return config
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error)
  },
)

// Add a response interceptor
axios.interceptors.response.use(
  function (response: AxiosResponse<BaseResponse<unknown>>) {
    if (!response.data.success) {
      showToast(response.data.msg)
    }
    return response
  },
  function (error) {
    showToast(error)
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error)
  },
)
export default instance
